﻿using DoxygenBrowser.Common.Parser;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;
using DoxygenBrowser.Common.Model;
using System.Text;
using System;
using System.Diagnostics;
using TestDoxTokenParser.Properties;

namespace TestDoxTokenParser
{


    [TestClass()]
    public class DoxTokenParserTest
    {
        public class TestLoader
        {
            private int _iNumEntries;

            public TestLoader()
            {
            }


            public void HandleTokenAdded(object sender, TokenAddedEventArgs e)
            {
                _iNumEntries++;
            }

            public int NumEntries
            {
                get { return _iNumEntries; }
            }

            private void DebugTokenEntry(TokenIdentifier tokenId, FileLocation fileLocation)
            {
                Debug.Print("---------------------------------------------------------------");
                Debug.Print("{0} {1} {2} {3}", tokenId.Name, tokenId.Scope, tokenId.Type, tokenId.APILanguage);
                Debug.Print("{0} {1}", fileLocation.Path, fileLocation.Anchor);
                Debug.Print("\n");
            }



        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        private TestLoader createTDictionaryFromString(string strXmlTokens)
        {
            TestLoader testLoader = new TestLoader();
            Stream memoryStream = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(strXmlTokens));
            DocsetParser target = new DocsetParser();
            target.TokenAddedEvent += new EventHandler<TokenAddedEventArgs>(testLoader.HandleTokenAdded);
            target.Parse(memoryStream);
            return testLoader;
        }

        private TestLoader parserFromFile(string fileContent)
        {
            TestLoader testLoader = new TestLoader();
            string path = AppDomain.CurrentDomain.BaseDirectory;

            byte[] byteArray = Encoding.ASCII.GetBytes(fileContent);
            MemoryStream stream = new MemoryStream(byteArray);

            DocsetParser target = new DocsetParser();
            target.TokenAddedEvent += new EventHandler<TokenAddedEventArgs>(testLoader.HandleTokenAdded);

            Assert.IsNotNull(stream);
            target.Parse(stream);

            return testLoader;

        }


        /// <summary>
        ///A test for Parser
        ///</summary>
        [TestMethod()]
        public void ParserTest()
        {
            TestLoader testLoader = createTDictionaryFromString(Resources.Tokens);
            Assert.IsTrue(testLoader.NumEntries == 20);
        }


        /// <summary>
        /// Parsers the empty token list test.
        /// </summary>
        [TestMethod()]
        public void Parser_EmptyTokenListTest()
        {
            TestLoader testLoader = createTDictionaryFromString(Resources.Tokens_EmptyList);
            Assert.IsTrue(testLoader.NumEntries == 0);
        }


        /// <summary>
        /// Parser one token list test.
        /// </summary>
        [TestMethod()]
        public void Parser_1TokenListTest()
        {

            TestLoader testLoader = createTDictionaryFromString(Resources.Tokens_1Token);
            Assert.IsTrue(testLoader.NumEntries == 1);

        }

        /// <summary>
        /// Parser one token list test.
        /// </summary>
        [TestMethod()]
        public void Parser_WithSpecialCharsTest()
        {
            TestLoader testLoader = parserFromFile(Resources.Tokens_SpecialChars);

            Assert.IsTrue(testLoader.NumEntries == 2);
        }


        /// <summary>
        /// Parser_s huge token list test.
        /// </summary>
        [TestMethod()]
        public void Parser_HugeTokenListTest()
        {
            string value = Directory.GetCurrentDirectory();
            TestLoader testLoader = parserFromFile(Resources.Tokens_huge);

            Assert.IsTrue(testLoader.NumEntries > 0);
        }


    }
}
